//
//  NSMutableAttributedString+JH_PriceTool.m
//  JiHe
//
//  Created by mac on 2017/3/7.
//  Copyright © 2017年 StepToWin. All rights reserved.
//

#import "NSMutableAttributedString+JH_PriceTool.h"
#import "Com_FontStringAttribute.h"

@implementation NSMutableAttributedString (JH_PriceTool)


/**
 获得 文本属性

 @param text 输入的文本
 @param defaultFont 默认的字体
 @param highlightedFont 突出的字体
 @return 文本属性
 */
+ (NSMutableAttributedString *)text:(NSString *)text
                        defaultFont:(UIFont *)defaultFont
                    highlightedFont:(UIFont *)highlightedFont {
    
    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc]initWithString:text];
    NSRange range = [text rangeOfString:@"￥[0-9]{1,}(\\.[0-9]{1,3})?" options:NSRegularExpressionSearch];
    if (range.location != NSNotFound) {
        
        //不包含￥
        NSString * priceStr  = [text substringWithRange:NSMakeRange(range.location+1, range.length-1)];
        NSArray  * priceArr  = [priceStr componentsSeparatedByString:@"."];
        //金额的标记
        NSString * moneyTagStr  = @"￥";//
        NSRange    moneyTagRange= NSMakeRange(range.location, moneyTagStr.length);
        [attributedStr addStringAttribute:[Com_FontStringAttribute font:defaultFont?:[UIFont systemFontOfSize:12] effectiveRange:moneyTagRange]];
        
        //金额的整数部分
        NSString * moneyIntegerStr  = priceArr.firstObject;
        NSRange    moneyIntegerRange= NSMakeRange(moneyTagRange.location+moneyTagRange.length, moneyIntegerStr.length);
        [attributedStr addStringAttribute:[Com_FontStringAttribute font:highlightedFont?:[UIFont systemFontOfSize:17] effectiveRange:moneyIntegerRange]];
        
        //金额的小数部分
        NSString * moneyDecimalStr  = [priceArr count]>1?[NSString stringWithFormat:@".%@",priceArr.lastObject]:nil;
        NSRange    moneyDecimalRange= NSMakeRange(moneyIntegerRange.location+moneyIntegerRange.length, moneyDecimalStr.length);
        [attributedStr addStringAttribute:[Com_FontStringAttribute font:defaultFont?:[UIFont systemFontOfSize:12] effectiveRange:moneyDecimalRange]];
    }
    return attributedStr;
}

@end
